The systemctl command has some frequently overlooked functionality. In addition to starting and stopping Linux services, you can list the installed services, and check what state they’re in. Here’s a quick run-through.
What Is the systemctl Command?
The systemctl command is the central management tool for the systemd init system, probably best known as the tool used to start and stop services. But there’s more to it than that, as evidenced by its man page being over 1600 lines in length.
Because systemctl is a management tool, not just a service launcher, you can use it to access useful information about your systemd system and services.
Most Linux distributions have adopted systemd, but some opted to keep the traditional SystemV init system. If you’re not sure which scheme your distribution uses, it’s a simple matter to find out. We’ll use the stat command to look at the init file.
stat /sbin/init
The executable file /sbin/init is the first process that is launched in SystemV-based distributions. On systemd-based distributions, a symbolic link with that name points to the systemd file.
The first line of output shows us that on this Ubuntu test machine, /sbin/init is a symbolic link to the /lib/systemd/systemd file. Plainly, this Linux installation uses systemd. If this was a SystemV-based distribution, the line would contain “File: /sbin/init” only.
Interrogating Services With systemctl
Services are defined in unit files, and you’ll see the word unit scattered throughout the systemctl options. As a point in case, we can obtain a list of services with the list-units command with the –type option.
systemctl list-units --type=service
The output is shown in the less file viewer, allowing you to scroll through the output, and to use the / key to search.
- Unit: The name of the unit file.
- Load: If the service’s unit file has been read into memory with no syntax errors, this column will contain “loaded.” This doesn’t mean the service is active.
- Active: A high-level view of whether a service is active. An active service might not be running.
- Sub: A more granular view of whether a service is running. For example, an active service might be timed to a timer, and may have exited its last execution run.
- Description: A line of text intended to identify or describe the service.
The display only includes active services. To see all services, we need to include the –all option.
systemctl list-units --all --type=service
If seeing everything is too much like information overload, we can filter the output with the –state option.
systemctl list-units --type=service --state=running
The state option will accept running, stopped, enabled, disabled, and failed.
To focus in on failed services, use the –failed option.
systemctl list-units --failed
There are no failed units on this computer.
If you do see any failed services, use the list-dependencies option to check if there are unmet dependencies.
systemctl list-dependencies sshd.service
The dependencies have a color-coded circle representing their state. It can be:
- White Circle: Inactive or maintenance
- Green Dot: Active.
- White Dot: Deactivating.
- Red Dot: Failed or error.
To check whether a single service is enabled, use the is-enabled command and provide the name of the service’s unit file.
systemctl is-enabled htg-example.service
Controlling Services With systemctl
Using systemctl to manage services is very straightforward, and follows the format of the commands we’ve seen so far. The biggest difference is you’ll need to use sudo to make changes to the states of services. We haven’t had to use it so far, because we’ve only been reporting on service states.
To start a service, use the start command followed by the name of the service.
sudo systemctl start htg-example.service
If all goes well, you’re returned silently to the command prompt. If you prefer to have a positive confirmation, you can get verification from the status command.
sudo systemctl status htg-example.service
Stopping a service is just as straightforward as starting one.
sudo systemctl stop htg-example.service
You can restart a service without having to go through the two-step process of manually stopping and then starting it. The restart command does it all for you.
sudo systemctl restart htg-example.service
If you want a service to start at boot time, you need to enable it.
sudo systemctl enable htg-example.service
Note that this just flags the service so that it gets started at boot time, it doesn’t start it straight away. If that’s what you want, add the –now flag.
sudo systemctl enable --now htg-example.service
When you no longer need a service to start at boot time, disable it.
sudo systemctl disable htg-example.service
You can use the journalctl command, another part of systemd, to look for entries relating to your service related in the . The -u (unit) option lets you specify the service you’re interested in. With the -S (since) option, you can show entries that have happened since the time you provide.
journalctl -S "08:00:00" -u htg-example.service
Anything that helps you gain insights into the inner workings of your Linux distribution is going to be a useful tool, for daily management and for troubleshooting and diagnosing issues. The systemctl command isn’t a single tool. It’s more like a tool chest of specialist tools, and well worth getting to grips with.
Source link